package com.air.ota.controller.imp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.common.controller.imp.BaseControllerImp;
import com.air.ota.controller.IInboundMsgController;
import com.air.ota.mo.CardSequence;
import com.air.ota.mo.InboundMsg;
import com.air.ota.service.ICancelConfirmHandlerService;
import com.air.ota.service.ICancelInitHandlerService;
import com.air.ota.service.ICancelResultHandlerService;
import com.air.ota.service.ICardSequenceService;
import com.air.ota.service.IDepositRequestHandlerService;
import com.air.ota.service.IDepositResultHandlerService;
import com.air.ota.service.IDepositSyncHandlerService;
import com.air.ota.service.IInboundMsgService;
import com.air.ota.service.IModifyPwdHandlerService;
import com.air.ota.service.IPkgSequenceService;
import com.air.ota.service.IRegRequestHandlerService;
import com.air.ota.service.IRegResultHandlerService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.vo.UTKBaseUplinkPgkVO;
import com.air.ota.vo.UTKCancelConfirmUplinkVO;
import com.air.ota.vo.UTKCancelInitUplinkVO;
import com.air.ota.vo.UTKCancelResultUplinkVO;
import com.air.ota.vo.UTKDepositRequestUplinkVO;
import com.air.ota.vo.UTKDepositResultUplinkVO;
import com.air.ota.vo.UTKDepositSyncUplinkVO;
import com.air.ota.vo.UTKModifyPwdUplinkVO;
import com.air.ota.vo.UTKRegRequestUplinkVO;
import com.air.ota.vo.UTKRegResultUplinkVO;

@Controller
@RequestMapping("/inboundMsg")
public class InboundMsgControllerImp extends BaseControllerImp implements
		IInboundMsgController {

	@Autowired
	IInboundMsgService inboundMsgService;
	@Autowired
	ICardSequenceService cardSequenceService;
	@Autowired
	IPkgSequenceService pkgSequenceService;
	
	@Autowired
	IDepositRequestHandlerService depositRequestHandlerService;
	@Autowired
	IDepositResultHandlerService  depositResultHandlerService;
	@Autowired
	IRegRequestHandlerService regRequestHandlerService;
	@Autowired
	IRegResultHandlerService  regResultHandlerService;
	@Autowired
	ICancelInitHandlerService cancelInitHandlerService;
	@Autowired
	ICancelResultHandlerService cancelResultHandlerService;
	@Autowired
	ICancelConfirmHandlerService cancelConfirmHandlerService;
	@Autowired
	IModifyPwdHandlerService modifyPwdHandlerService;
	
	@Autowired
	IDepositSyncHandlerService depositSyncHandlerService;
	
	@Override
	@RequestMapping("process/{msgId}")
	public ModelAndView process(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer msgId) throws Exception {
		
		InboundMsg msg =inboundMsgService.findById(msgId);
		byte[] pkgBytes = HexStringUtil.hexStringToBytes(msg.getContent());
		System.out.println("-------------------------SMS Content---------------------");
		System.out.println(msg.getContent());
		System.out.println("-------------------------SMS Content---------------------");
		
		UTKBaseUplinkPgkVO uplinkPkgVO = new UTKBaseUplinkPgkVO();
		uplinkPkgVO.parse(pkgBytes);
		
		byte cmdType = uplinkPkgVO.getCmdType();
		
		byte[] cardSequence=null;
		CardSequence cardSequenceMO=cardSequenceService.findByPhoneNum(msg.getPhoneNumber());
		if (cardSequenceMO!=null){
			cardSequence = HexStringUtil.hexStringToBytes(cardSequenceMO.getCardSequence());
		}
		int pkgSequence;
		int pkgSequenceInDB;
		switch (cmdType){
			case 0x04://开通认证
				UTKRegRequestUplinkVO regRequestpkgVO = new UTKRegRequestUplinkVO();
				regRequestpkgVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(regRequestpkgVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), regRequestpkgVO.getPkgSequence());
				regRequestHandlerService.process(regRequestpkgVO, msg);
				break;
			case 0x05://开通结果
				UTKRegResultUplinkVO regResultpkgVO = new UTKRegResultUplinkVO(cardSequence); 
				regResultpkgVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(regResultpkgVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), regResultpkgVO.getPkgSequence());
				regResultHandlerService.process(regResultpkgVO, msg);
				break;
			case 0x01://圈存请求
				UTKDepositRequestUplinkVO depositRequestPkgVO = new UTKDepositRequestUplinkVO(cardSequence);
				depositRequestPkgVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(depositRequestPkgVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), depositRequestPkgVO.getPkgSequence());
				depositRequestHandlerService.process(depositRequestPkgVO, msg);
				break;
			case 0x02://圈存结果
				UTKDepositResultUplinkVO depositResultPkgVO = new UTKDepositResultUplinkVO(cardSequence);
				depositResultPkgVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(depositResultPkgVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), depositResultPkgVO.getPkgSequence());
				depositResultHandlerService.process(depositResultPkgVO, msg);
				break;
			case 0x08://注销扣款初始化请求
				UTKCancelInitUplinkVO utkCancelnitUplinkVO = new UTKCancelInitUplinkVO(cardSequence);
				utkCancelnitUplinkVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(utkCancelnitUplinkVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), utkCancelnitUplinkVO.getPkgSequence());
				this.cancelInitHandlerService.process(utkCancelnitUplinkVO, msg);
				break;
			case 0x09://注销扣款结果
				UTKCancelResultUplinkVO utkCancelResultUplinkVO = new UTKCancelResultUplinkVO(cardSequence);
				utkCancelResultUplinkVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(utkCancelResultUplinkVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), utkCancelResultUplinkVO.getPkgSequence());
				this.cancelResultHandlerService.process(utkCancelResultUplinkVO, msg);
				break;
			case 0x0A://注销结果
				UTKCancelConfirmUplinkVO utkCancelConfirmUplinkVO = new UTKCancelConfirmUplinkVO(cardSequence);
				utkCancelConfirmUplinkVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(utkCancelConfirmUplinkVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), utkCancelConfirmUplinkVO.getPkgSequence());
				this.cancelConfirmHandlerService.proces(utkCancelConfirmUplinkVO, msg);
				break;
			case 0x0B://支付密码修改
				UTKModifyPwdUplinkVO utkModifyPwdUplinkVO = new UTKModifyPwdUplinkVO(cardSequence);
				utkModifyPwdUplinkVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(utkModifyPwdUplinkVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), utkModifyPwdUplinkVO.getPkgSequence());
				this.modifyPwdHandlerService.process(utkModifyPwdUplinkVO, msg);
				break;
			case 0x03://钱包圈存同步请求
				UTKDepositSyncUplinkVO utkDespositSyncUplinkVO = new UTKDepositSyncUplinkVO(cardSequence);
				utkDespositSyncUplinkVO.parse(pkgBytes);
				
				pkgSequence = Integer.parseInt(HexStringUtil.bytesToHexString(utkDespositSyncUplinkVO.getPkgSequence()),16);
				pkgSequenceInDB = this.pkgSequenceService.getCurrentSequence(msg.getPhoneNumber());
				if (pkgSequence>pkgSequenceInDB){
					System.out.println("ERROR!!! DISCARD SMS Cause: pkgSequence in SMS="+pkgSequence+" is large than pkgSequence in DB="+pkgSequenceInDB);
					return null;
				}
				
				pkgSequenceService.setCurrentSequence(msg.getPhoneNumber(), utkDespositSyncUplinkVO.getPkgSequence());
				depositSyncHandlerService.process(utkDespositSyncUplinkVO, msg);
				break;
			default:
				System.out.println("UNKNOWN CmdType=0x"+HexStringUtil.byteToHexString(cmdType));
		}
		
		return null;
	}
	
	
	
	
}
